package com.imphead.demo1.config;

import org.jboss.logging.MDC;

import javax.servlet.*;
import java.io.IOException;
import java.util.UUID;

public class LogMDCFilter implements Filter {
    private static final String UNIQUE_ID_NAME = "traceId";

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {
        MDC.put(UNIQUE_ID_NAME, UUID.randomUUID().toString().replace("-", ""));// UUID太长，可自定义配置短的，短时间内不重复就行
        try {
            filterChain.doFilter(servletRequest, servletResponse);
        } finally {
            MDC.remove(UNIQUE_ID_NAME);
        }
    }
}
